/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.util.pager;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.ejb.facade.representative.RepresentativeFacadeLocal;

/**
 *
 * @author makcro
 */
public class RepresentativePager extends AbstractPager {


    private String nameFilter;
    private String nameOperator;
    private String telephoneFilter;
    private String telephoneOperator;
    private String faxFilter;
    private String faxOperator;
    private String emailFilter;
    private String emailOperator;
    private String functionFilter;
    private String functionOperator;
    private String[] institutionFilter;
    private String[] iTypeFilter;
    private String[] iRegionFilter;
    private String[] iFamilyFilter;
    private String[] iCityFilter;
    private String orderBy;
    

    public RepresentativePager(String nameFilter,
            String nameOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String emailFilter,
            String emailOperator,
            String functionFilter,
            String functionOperator,
            String[] institutionFilter,
            String[] iTypeFilter,
            String[] iRegionFilter,
            String[] iFamilyFilter,
            String[] iCityFilter,
            String orderBy
            
            ) {
        this.nameFilter = nameFilter;
        this.nameOperator = nameOperator;
        this.telephoneFilter = telephoneFilter;
        this.telephoneOperator = telephoneOperator;
        this.faxFilter = faxFilter;
        this.faxOperator = faxOperator;
        this.emailFilter = emailFilter;
        this.emailOperator = emailOperator;
        this.functionFilter = functionFilter;
        this.functionOperator = functionOperator;
        this.institutionFilter = institutionFilter;
        this.iTypeFilter = iTypeFilter;
        this.iRegionFilter = iRegionFilter;
        this.iFamilyFilter = iFamilyFilter;
        this.iCityFilter = iCityFilter;
        this.orderBy = orderBy;


        resultsTotal = lookupRepresentativeFacade().getSearchResultCount(nameFilter, nameOperator, telephoneFilter, telephoneOperator, faxFilter, faxOperator, emailFilter, emailOperator, functionFilter, functionOperator, institutionFilter, iTypeFilter, iRegionFilter, iFamilyFilter, iCityFilter, orderBy);

    }










    @Override
    protected void injectData(HttpServletRequest request) {
        List<Representative> reps = lookupRepresentativeFacade().search(nameFilter, nameOperator, telephoneFilter, telephoneOperator, faxFilter, faxOperator, emailFilter, emailOperator, functionFilter, functionOperator, institutionFilter, iTypeFilter, iRegionFilter, iFamilyFilter, iCityFilter, orderBy, firstResult, maxResultsPerPage, false);

        request.setAttribute("REP_SEARCH_RESULTS", reps);
        request.setAttribute("REP_SEARCH_PAGENUMBERS", generatePageNumbers());
        request.setAttribute("REP_SEARCH_TOTAL_RESULTS", this.resultsTotal);
        request.setAttribute("REP_SEARCH_SHOW_NEXT", new Boolean(this.showNext));
        request.setAttribute("REP_SEARCH_SHOW_PREV", new Boolean(this.showPrev));


    }

    @Override
    protected void setPageSize() {
        maxResultsPerPage = 20;
    }


    private RepresentativeFacadeLocal lookupRepresentativeFacade() {
        try {
            Context c = new InitialContext();
            return (RepresentativeFacadeLocal) c.lookup("java:comp/env/RepresentativeFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
